<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/component/pear/css/pear.css" />
    <script src="/static/component/layui/layui.js"></script>
    <script src="/static/component/pear/pear.js"></script>
</head>

<body class="pear-container">
    <!-- <div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
                           
               <div class="layui-form-item layui-inline">
                   <label class="layui-form-label">服务器ID</label>
                   <div class="layui-input-inline">
                       <input type="text" name="sid" placeholder="" class="layui-input">
                   </div>
               </div>   
               <div class="layui-form-item layui-inline">
                   <label class="layui-form-label">服务器名</label>
                   <div class="layui-input-inline">
                       <input type="text" name="name" placeholder="" class="layui-input">
                   </div>
               </div>   
               <div class="layui-form-item layui-inline">
                   <label class="layui-form-label">服务器状态</label>
                   <div class="layui-input-inline">
                       <input type="text" name="status" placeholder="" class="layui-input">
                   </div>
               </div>   
               <div class="layui-form-item layui-inline">
                   <label class="layui-form-label">服务器IP</label>
                   <div class="layui-input-inline">
                       <input type="text" name="host" placeholder="" class="layui-input">
                   </div>
               </div>   
               <div class="layui-form-item layui-inline">
                   <label class="layui-form-label">服务器端口</label>
                   <div class="layui-input-inline">
                       <input type="text" name="port" placeholder="" class="layui-input">
                   </div>
               </div>
                        <div class="layui-form-item layui-inline">
                       <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="query">
                           <i class="layui-icon layui-icon-search"></i>
                           查询
                       </button>
                       <button type="reset" class="pear-btn pear-btn-md">
                           <i class="layui-icon layui-icon-refresh"></i>
                           重置
                       </button>
                       </div>
                    </div>
				</form>
			</div>
		</div> -->
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>

    <script type="text/html" id="toolbar">
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-survey"></i>
		        冠名
			</button>
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="edit">
		        <i class="layui-icon layui-icon-survey"></i>
		        角色转职
		    </button>
            <!-- <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-refresh-1"></i>
		        重启
		    </button> -->
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-edit"></i>
		        服务器状态修改
		    </button>

            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="addNewServer1">
		        <i class="layui-icon layui-icon-edit"></i>
		        开神谕
		    </button>
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="addNewServer2">
                <i class="layui-icon layui-icon-edit"></i>
                开重生
            </button>
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="addNewServer3">
                <i class="layui-icon layui-icon-edit"></i>
                开序列
            </button>

            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="choujiang">
                <i class="layui-icon layui-icon-edit"></i>
                抽奖测试
            </button>
			<!-- <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增
			</button>
			<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        删除
		    </button>
            <button class="pear-btn pear-btn-md" lay-event="recycle">
		        <i class="layui-icon layui-icon-delete"></i>
		        回收站
		    </button> -->
		</script>


    <script type="text/html" id="status">
            {{# if(d.status == 0){ }}
                <span class="layui-badge-dot layui-bg-red"></span>维护
            {{# } else if(d.status == 1){ }}
                <span class="layui-badge-dot layui-bg-blue"></span>新区
            {{# } else if(d.status == 2){ }}
                <span class="layui-badge-dot layui-bg-orange"></span>火爆
            {{# } }}
        </script>

    <script type="text/html" id="options">
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="closeServer">关服</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="openServer">开服</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="closekf">关跨服</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="openkf">开跨服</a>
		</script>


    <script>
        layui.use(['table', 'form', 'jquery', 'common', 'laydate'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let common = layui.common;
            let laydate = layui.laydate;
            let MODULE_PATH = "{$Request.root}/h5.server/";

            let cols = [
                [{
                    type: 'checkbox'
                }, {
                    field: "id",
                    title: "id",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "sid",
                    title: "服务器ID",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "name",
                    title: "服务器名",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "status",
                    title: "服务器状态",
                    unresize: "true",
                    align: "center",
                    templet: "#status"
                }, {
                    field: "host",
                    title: "服务器IP",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "port",
                    title: "服务器端口",
                    unresize: "true",
                    align: "center"
                }, {
                    title: '操作',
                    toolbar: '#options',
                    align: 'center',
                    unresize: true,
                    width: 280
                }

                ]
            ]

            table.render({
                elem: '#dataTable',
                url: MODULE_PATH + 'index',
                page: true,
                cols: cols,
                limit: 10,
                limits: [10, 30, 50, 100, 200, 500, 1000],
                height: 'full-50',
                cellMinWidth: 100,
                skin: 'line',
                toolbar: '#toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            table.on('tool(dataTable)', function (obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                } else if (obj.event === 'closeServer') {
                    window.closeServer(obj);
                } else if (obj.event === 'openServer') {
                    window.openServer(obj);
                } else if (obj.event === 'openkf') {
                    window.openkf(obj);
                } else if (obj.event === 'closekf') {
                    window.closekf(obj);
                }
            });

            table.on('toolbar(dataTable)', function (obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'batchRemove') {
                    window.batchRemove(obj);
                } else if (obj.event === 'recycle') {
                    window.recycle(obj);
                } else if (obj.event === 'edit') {
                    window.edit();
                } else if (obj.event === 'addNewServer1') {
                    window.addNewServer(1);
                } else if (obj.event === 'addNewServer2') {
                    window.addNewServer(2);
                } else if (obj.event === 'addNewServer3') {
                    window.addNewServer(3);
                }else if (obj.event === 'choujiang') {
                    window.choujiang();
                }
            });

            form.on('submit(query)', function (data) {
                table.reload('dataTable', {
                    where: data.field,
                    page: { curr: 1 }
                })

                return false;
            });

            form.on("switch(status)", function (data) {
                var status = data.elem.checked ? 1 : 2;
                var id = this.value;
                var load = layer.load();
                $.post(MODULE_PATH + "status", { status: status, id: id }, function (res) {
                    layer.close(load);
                    //判断有没有权限
                    if (res && res.code == 999) {
                        layer.msg(res.msg, {
                            icon: 5,
                            time: 2000,
                        })
                        return false;
                    } else if (res.code == 200) {
                        layer.msg(res.msg, { icon: 1, time: 1500 })
                    } else {
                        layer.msg(res.msg, { icon: 2, time: 1500 }, function () {
                            $(data.elem).prop("checked", !$(data.elem).prop("checked"));
                            form.render()
                        })
                    }
                })
            });
            //弹出窗设置 自己设置弹出百分比
            function screen() {
                // if (typeof width !== 'number' || width === 0) {
                // width = $(window).width() * 0.8;
                // }
                // if (typeof height !== 'number' || height === 0) {
                // height = $(window).height() - 20;
                // }
                // return [width + 'px', height + 'px'];
                return ['700px', '450px'];
            }

            window.add = function () {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '区服冠名',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'add'
                });
            }

            window.edit = function () {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '修改游戏服管理',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'edit'
                });
            }

            window.choujiang = function () {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '抽奖发放',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'choujiang'
                });
            }

            window.recycle = function () {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '回收站',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'recycle',
                    cancel: function () {
                        table.reload('dataTable');
                    }
                });
            }

            window.closeServer = function (obj) {
                layer.confirm('确定要关闭该游戏服!', { icon: 3, title: '提示' }, function (index) {
                    console.log(obj);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + 'closeServer',
                        data: { sid: obj.data['sid'] },
                        dataType: 'json',
                        type: 'POST',
                        success: function (res) {
                            layer.close(loading);
                            layer.msg(res.msg);
                        }
                    })
                })
            }
            window.openServer = function (obj) {
                layer.confirm('确定要开启该游戏服!', { icon: 3, title: '提示' }, function (index) {
                    console.log(obj);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + 'openServer',
                        data: { sid: obj.data['sid'] },
                        dataType: 'json',
                        type: 'POST',
                        success: function (res) {
                            layer.close(loading);
                            layer.msg(res.msg);
                        }
                    })
                })
            }
            window.openkf = function (obj) {
                layer.confirm('确定要开启该游戏跨服!', { icon: 3, title: '提示' }, function (index) {
                    console.log(obj);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + 'openkf',
                        data: { sid: obj.data['sid'] },
                        dataType: 'json',
                        type: 'POST',
                        success: function (res) {
                            layer.close(loading);
                            layer.msg(res.msg);
                        }
                    })
                })
            }
            window.closekf = function (obj) {
                layer.confirm('确定要关闭该游戏跨服!', { icon: 3, title: '提示' }, function (index) {
                    console.log(obj);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + 'closekf',
                        data: { sid: obj.data['sid'] },
                        dataType: 'json',
                        type: 'POST',
                        success: function (res) {
                            layer.close(loading);
                            layer.msg(res.msg);
                        }
                    })
                })
            }


            window.remove = function (obj) {
                layer.confirm('确定要删除该游戏服管理', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + 'remove',
                        data: { id: obj.data['id'] },
                        dataType: 'json',
                        type: 'POST',
                        success: function (res) {
                            layer.close(loading);
                            //判断有没有权限
                            if (res && res.code == 999) {
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000,
                                })
                                return false;
                            } else if (res.code == 200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    obj.del();
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.batchRemove = function (obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                var ids = []
                var hasCheck = table.checkStatus('dataTable')
                var hasCheckData = hasCheck.data
                if (hasCheckData.length > 0) {
                    $.each(hasCheckData, function (index, element) {
                        ids.push(element.id)
                    })
                }
                layer.prompt({ title: '输入状态(0.维护||1.正常||2.新区)', formType: 2 }, function (text, index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + 'batchRemove',
                        data: { ids: ids, status: text },
                        dataType: 'json',
                        type: 'POST',
                        success: function (res) {
                            layer.close(loading);
                            //判断有没有权限
                            if (res && res.code == 999) {
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000,
                                })
                                return false;
                            } else if (res.code == 200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    table.reload('dataTable');
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })

                });

            }

            window.addNewServer = function (key) {
                layer.confirm('确认要添加新区？', { icon: 0 }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        type: "POST",
                        data:{key:key},
                        cache: true,
                        url: MODULE_PATH + 'addNewServer',
                        success: function (data) {
                            console.log(data)
                            layer.close(loading);
                            if (data.code == 200) {
                                layer.msg(data.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    table.reload('dataTable');
                                });
                            } else {
                                layer.alert(data.msg, { icon: 0, anim: 6 });
                            }
                        }
                    });
                });
            }



            window.refresh = function (param) {
                table.reload('dataTable');
            }
        })
    </script>
</body>

</html>